using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Authorities.Operations;
using Wips.AppService.Authorities.Operations.Requests;
using Wips.AppService.Authorities.Operations.Responses;
using Wips.AppService.Authorities.Operations.ViewModels;

namespace Wips.MainServer.Controllers.Authorities
{
    /// <summary>
    /// 操作
    /// </summary>
    public class OperationController : CustomeBaseController
    {
        private readonly IOperationService _service;
        private readonly IMediator _mediator;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="mediator"></param>
        public OperationController(
            IOperationService service,
            IMediator mediator)
        {
            _service = service;
            _mediator = mediator;
        }

        #region 基础接口

        /// <summary>
        /// 读取列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<PageResult<GetOperationPagesResponse>> GetPagesAsync(
            [FromBody] GetOperationPagesRequest request)
        {
            return await _service.GetPagesAsync(request);
        }

        /// <summary>
        /// 根据ID读取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<OperationViewModel> GetAsync([FromQuery] string id)
        {
            return await _service.GetByIdAsync(id);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> PostAsync([FromBody] CreateOperationRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> PutAsync([FromBody] UpdateOperationRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission]
        public async Task<string> DeleteAsync([FromBody] DeleteOperationRequest request)
        {
            return await _mediator.Send(request);
        }

        #endregion

        #region 扩展接口

        /// <summary>
        /// 读取权限代码
        /// </summary>
        /// <param name="userId">用户Id，默认为当前登录用户</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<string>> GetCodesByUserIdAsync(string userId = "")
        {
            return await _service.GetCodesByUserIdAsync(userId);
        }

        #endregion
    }
}